import mcApi from 'mc-c2b2m-operator-api';
import { TreeSelect } from 'ant-design-vue';
import { priceUnitList } from '@/enums/spuEnum';
import { BasicColumn, FormSchema } from '@/components/Table';
import { MapQuotationBizMetaAttrModel } from 'mc-c2b2m-operator-api/types/spu';

const { SpuModule } = mcApi;
const { BizMetaArrApi } = SpuModule;

export const tableColumns: BasicColumn[] = [
  { title: '名称', dataIndex: 'cnName' },
  { title: '关键字', dataIndex: 'keyCnName' },
  {
    title: '业务元属性',
    dataIndex: 'bizMetaAttrList',
    customRender: ({ text }) => {
      return text?.map?.((item: MapQuotationBizMetaAttrModel) => item.cnName)?.join?.('、');
    },
  },
  {
    title: '关键字业务元属性',
    dataIndex: 'keyBizMetaAttrList',
    customRender: ({ text }) => {
      return text?.map?.((item: MapQuotationBizMetaAttrModel) => item.cnName)?.join?.('、');
    },
  },
  {
    title: '计价单位',
    dataIndex: 'priceUnit',
    customRender: ({ text }) => {
      return priceUnitList.find((find) => find.value === text)?.label;
    },
  },
  { title: '创建时间', dataIndex: 'createdTime' },
];
export const tableFormSchema: FormSchema[] = [
  {
    field: 'cnName',
    label: '名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'keyCnName',
    label: '关键字',
    component: 'Input',
    colProps: { span: 6 },
  },
];

const bizMetaAttrSchema: FormSchema = {
  required: true,
  field: 'bizMetaAttrIdList',
  label: '业务元属性',
  component: 'ApiTreeSelect',
  componentProps: {
    showSearch: true,
    valueField: 'id',
    labelInValue: true,
    labelField: 'cnName',
    treeCheckStrictly: true,
    treeDefaultExpandAll: true,
    treeNodeFilterProp: 'cnName',
    childrenField: 'childBizMetaAttrList',
    api: BizMetaArrApi.getBizMetaAttrTree,
    showCheckedStrategy: TreeSelect.SHOW_ALL,
  },
};

export const modalFormSchema: FormSchema[] = [
  {
    ifShow: false,
    field: 'id',
    label: 'id',
    component: 'Input',
  },
  {
    ifShow: false,
    field: 'quotationType',
    label: 'quotationType',
    component: 'Input',
  },
  {
    required: true,
    field: 'cnName',
    label: '名称',
    component: 'Input',
  },
  {
    required: true,
    field: 'keyCnName',
    label: '关键字',
    component: 'Input',
  },
  {
    ...bizMetaAttrSchema,
    field: 'bizMetaAttrIdList',
    label: '业务元属性',
  },
  {
    ...bizMetaAttrSchema,
    field: 'keyBizMetaAttrIdList',
    label: '关键字业务元属性',
  },
  {
    required: true,
    field: 'priceUnit',
    label: '计价单位',
    component: 'Select',
    componentProps: {
      options: priceUnitList,
    },
  },
];

export const detailTableColumn: BasicColumn[] = [
  { title: 'id', dataIndex: 'id' },
  {
    title: '业务元属性',
    dataIndex: 'bizMetaAttrList',
    customRender: ({ text }) => {
      return text?.map?.((item: MapQuotationBizMetaAttrModel) => item.cnName)?.join?.('、');
    },
  },
  {
    title: 'KEY值业务元属性',
    dataIndex: 'keyValueBizMetaAttrList',
    customRender: ({ text }) => {
      return text?.map?.((item: MapQuotationBizMetaAttrModel) => item.cnName)?.join?.('、');
    },
  },
  { title: '映射KEY值', dataIndex: 'price' },
  { title: '创建时间', dataIndex: 'createdTime' },
];

export const detailModalFormSchema: FormSchema[] = [
  {
    ifShow: false,
    component: 'Input',
    label: 'id',
    field: 'id',
  },
  {
    label: '映射KEY业务元属性',
    slot: 'mapQuotationListId',
    field: 'mapQuotationListId',
  },
  {
    ...bizMetaAttrSchema,
    field: 'bizMetaAttrIdList',
    label: '业务元属性',
  },
  {
    ...bizMetaAttrSchema,
    field: 'keyValueBizMetaAttrIdList',
    label: 'KEY值业务元属性',
  },
  {
    required: true,
    label: '映射KEY值',
    field: 'price',
    slot: 'price',
  },
];
